package com.jianying.day08;

import com.jianying.beans.WaterSensor;
import com.jianying.func.WaterSensorMapFunction;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.state.AggregatingState;
import org.apache.flink.api.common.state.AggregatingStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

/**
 * ClassName: Flink01_KeyedState_ValueState_1
 * Package: com.atguigu.day08
 * Description:
 *
 * @Author: tubage
 * @Create: 2024/4/9 09:40
 * @Version: 1.0
 */
public class Flink06_KeyedState_AggregatingState {
    public static void main(String[] args) throws Exception {
        // TODO 统计每种传感器，平均水位
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        SingleOutputStreamOperator<WaterSensor> mapDS = env.socketTextStream("localhost", 8888)
                .map(new WaterSensorMapFunction());

        // TODO 按键分组 ID
        KeyedStream<WaterSensor, String> keyByDS = mapDS.keyBy(WaterSensor::getId);

        // TODO 分组后数据处理
       keyByDS.process(
               new KeyedProcessFunction<String, WaterSensor, String>() {
                    AggregatingState<Integer, Double> vcAggState;
                   @Override
                   public void open(Configuration parameters) throws Exception {
                       AggregatingStateDescriptor<Integer, Tuple2<Integer,Integer>, Double> aggregatingStateDescriptor =
                               new AggregatingStateDescriptor<Integer, Tuple2<Integer,Integer>, Double>("vcAggState",
                                       new AggregateFunction<Integer,  Tuple2<Integer,Integer>, Double>(){
                                           @Override
                                           public Tuple2<Integer, Integer> createAccumulator() {
                                               return Tuple2.of(0,0);
                                           }

                                           @Override
                                           public Tuple2<Integer, Integer> add(Integer value, Tuple2<Integer, Integer> accum) {
                                               return Tuple2.of(accum.f0 + value, accum.f1 + 1);
                                           }

                                           @Override
                                           public Double getResult(Tuple2<Integer, Integer> accum) {
                                               return (double) (accum.f0 / accum.f1);
                                           }

                                           @Override
                                           public Tuple2<Integer, Integer> merge(Tuple2<Integer, Integer> integerIntegerTuple2, Tuple2<Integer, Integer> acc1) {
                                               return null;
                                           }
                                       },
                                       Types.TUPLE(Types.INT, Types.INT))

;                       vcAggState = getRuntimeContext().getAggregatingState(aggregatingStateDescriptor);
                   }

                   @Override
                   public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {
                       Integer vc = value.getVc();
                       vcAggState.add(vc);
                       Double avg = vcAggState.get();
                       out.collect("传感器：" + ctx.getCurrentKey() + ",水位值平均值:" + avg);
                   }
               }
       ).printToErr();

        env.execute();
    }
}
